package com.danguo.happymonkey;

import java.util.ArrayList;

//import android.graphics.Bitmap;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
//import android.opengl.GLUtils;
import android.opengl.Matrix;
//import android.util.Log;

public class Points
{
   /** How close the finger needs to be to the center of a point */
   private static final float TOUCH_PRECISION = 30f;

   private float[] theViewProjMatrix = new float[16];
   private ArrayList<StarSystem> thePoints;
  
   public enum Star {
      DENEB, ORION, ALTAIR
   }
   
   /**
    * @param reference to the context
    */
   public Points( GLSurfaceView aContext )
   { 
      thePoints = new ArrayList<StarSystem>();
      
      float[] testpoint1 = { -3f, 0f, 1f, 1f };
      thePoints.add( new StarSystem( aContext, testpoint1, Star.DENEB ) );
      float[] testpoint2 = { 1f, 1f, 5f, 1f };
      thePoints.add( new StarSystem( aContext, testpoint2, Star.ORION ) );
      float[] testpoint3 = { 2f, -1f, -4f, 1f };
      thePoints.add( new StarSystem( aContext, testpoint3, Star.ALTAIR ) );
   }
   
   /**
    * Draws all of the points given a view and proj matrix
    * @param aViewMatrix
    * @param aProjectionMatrix
    */
   public void draw( float[] aViewMatrix, float[] aProjectionMatrix )
   {
      Matrix.multiplyMM( theViewProjMatrix, 0, aProjectionMatrix, 0, aViewMatrix, 0 );
      
      GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
      GLES20.glEnable(GLES20.GL_BLEND); 
      
      for( int i = 0 ; i < thePoints.size() ; i++ )
      {
         thePoints.get( i ).draw( theViewProjMatrix );
      }
      
      GLES20.glDisable( GLES20.GL_BLEND );
   }
   
   /**
    * Finds a point at a screen coordinate
    * @param anX Screen coordinate x
    * @param aY Screen coordinate y
    * @param aScreenHeight The height of the context view
    * @param aScreenWidth The width of the context view
    * @return the system associated with the given screen coordinate
    */
   public int getSystemAt( float anX, float aY, int aScreenHeight, int aScreenWidth )
   {
      for( int i = 0 ; i < thePoints.size() ; i++ )
      {
         int[] p = thePoints.get( i ).getScreenCoords( theViewProjMatrix, 
                                                       aScreenHeight, aScreenWidth );
         if( Math.abs( p[0] - anX ) < TOUCH_PRECISION && 
             Math.abs( p[1] - aY  ) < TOUCH_PRECISION )
         {
            return i;
         }
      }
      return -1;
   }
   
/*   private void assignTexture( Bitmap anImg, int[] aTextureArray, int anIndex )
   {
      GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, aTextureArray[anIndex]);
      GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
      GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
      GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S,
            GLES20.GL_CLAMP_TO_EDGE);
      GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T,
            GLES20.GL_CLAMP_TO_EDGE);
      GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, anImg, 0);
      Log.d("LoadTexture", "Loaded texture"+":H:"+anImg.getHeight()+":W:"+anImg.getWidth());
   }*/
}
